<?php
/**
 * Note: *
 */
namespace App\Structs;

class Base
{
    public function __construct($data)
    {
        foreach ($data as $field => $value) {
            if (property_exists($this, $field)) {
                $this->$field = $value;
            }
        }
    }

    public function __toString(): string
    {
        $class = new \ReflectionClass($this);

        $properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);

        $arr = [];
        foreach ($properties as $property) {
            $key = $property->getName();
            $arr[$property->getName()] = $this->$key;
        }
        return json_encode($arr);
    }

    // public function __call($name)
    // {
    //     if (Str::startsWith($name, 'get') && $this->$name) {
    //         return $this->$name;
    //     }
    //     throw new \Exception('method not found: '. $name);
    // }
}
